package com.atguigu.worktest;

import com.atguigu.bean.UserBehavior;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class Flink01_UV {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);


        DataStreamSource<String> ds = env.readTextFile("input/UserBehavior.csv");
        ds.map(new MapFunction<String, UserBehavior>() {
            @Override
            public UserBehavior map(String value) throws Exception {
                String[] lines = value.split(",");
                UserBehavior userBehavior = new UserBehavior(Long.valueOf(lines[0]),
                        Long.valueOf(lines[1]),
                        Integer.valueOf(lines[2]),
                        String.valueOf(lines[3]), Long.valueOf(lines[4]));
                return userBehavior;
            }
        })
                .process(new ProcessFunction<UserBehavior, Long>() {
                    @Override
                    public void processElement(UserBehavior value, Context ctx, Collector<Long> out) throws Exception {
                        //我们是要求用户登录的次数，那么就需要进行一个去重的操作，这里我们将数据写入到数据库中
                    }
                });

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
